/**
 * @author yzhou2
 */


var QS_OverviewNews=function(container,config,callback){
    var now=new Date();
    this.container=container||$(document.body);
    this.config={
        cutLength:150,
        maxCount:5,
        type:'market',
        currentViewID:'-1',
        //instCode:'QS',
        fromDate:QS_DateFormat.formatDate(new Date(now.getFullYear(),now.getMonth(),now.getDate()-7)),
        toDate:QS_DateFormat.formatDate(now),
        title:'Markets & Economy',
        symbol:null,
        fixHeight:true,
        showTitle:true,
		needDateRange:true// if need pass the date range to search news
    };
    if(config){
        $.extend(this.config,config);
    }
    this.callback=callback||{};
    this.cache=null;
    this.el=null;
    this.headEl=null;
    this.headHight=null;
    this.contentEl=null;
    this.contentScrollPN=null;
    this.minWidth=250;
    this.html='<div class="qs-overview-news">'+(this.config.showTitle?('<div class="qs-overview-news-hd">'+this.config.title+'</div>'):'')+'<div class="qs-overview-news-bd"><div class="data"></div></div></div>'
    this.init();
};
QS_OverviewNews.prototype={
    init:function(){
        var self=this;
        this.el=$(this.html).appendTo(this.container);
        this.headEl=this.el.find('.qs-overview-news-hd');
        this.headHight=this.headEl.outerHeight(true);//this.headEl.height()+parseInt(this.headEl.css('paddingTop'))+parseInt(this.headEl.css('paddingBottom'));
        this.contentEl=this.el.find('.qs-overview-news-bd .data');
        if (this.config.fixHeight) {
            this.contentScrollPN = new Util.ScrollPanel(this.contentEl);
        }
        this.contentEl.bind('click.newOverview',function(e){
            var target=$(e.target);
            if(target.hasClass("title")) {
                var id = target.attr("vid");
                var d=self.cache[id];
                if (d) {
                    if (typeof self.callback.onNewsClick == 'function') {
                        self.callback.onNewsClick(id,self);
                    }
                }
            }else if(target.hasClass("more")&&typeof self.callback.onClickMore=='function'){
                self.callback.onClickMore(self);
            }
        });
        this._searchNews();
    },
    
    _getDefaultSourcesPara:function(id){
        var sources={},nts,dcs;
        var s=GB_NewsDataManager.defaultViewSource[id];
        if(typeof s!='undefined'){
            nts=s["ntSource"],dcs=s["dcSource"];
        }
        if(nts!=null&&nts.length!=0){
            sources["ntSourceCode"]=nts.join(',');
        }
        if(dcs!=null&&dcs.length!=0){
            sources["dcSourceCode"]=dcs.join(',');
        }
        return sources;
    },
    _searchNews:function(){
        var self=this;
        var postParas={
            cutLength:this.config.cutLength,
            maxCount:this.config.maxCount,
            //productCode:this.config.instCode=='QSDEMO'?'QS':this.config.instCode,
			productCode:'QS',
            lang:'en',
            type:this.config.type
        }
		if(this.config.needDateRange){
			postParas['fromDate']=this.config.fromDate;
			postParas['toDate']=this.config.toDate;
		}
        $.extend(postParas,self._getDefaultSourcesPara(this.config.currentViewID));
        GB_NewsDataManager.getNewsOverview(postParas, function(o){
            if (o == null || o == '') {
                self.fillNews(null);
            }
            else {
                var list = eval("(" + o + ")");
                self.fillNews(list);
            }
        });
    },
    fillNews:function(list){
        this.cache={};
        if(list==null||list.length==0){
            this.contentEl[0].innerHTML='No news available.';
        }else{
            var html='',url='';
            for(var i=0,l=list.length;i<l;i++){
                var d=list[i];
                var date=QS_DateFormat.toESTTime(d.dTime)+' EDT';
                this.cache[d.cId]={
                    pTeam:d.pTeam,
                    tpl:d.pTeam=='NT'?d.tpl:d.sName,
                    vendor:d.vendor,
                    source:d.sName,
                    date:date
                }
                if(i!=l-1){
                    html+='<div class="row"><div class="title" vid='+d.cId+'>'+d.title+'</div><div class="datetime">'+date+'</div><div class="detail">'+d.content+'...</div></div>'
                }else{
                    html+='<div class="row"><div class="title" vid='+d.cId+'>'+d.title+'</div><div class="datetime">'+date+'</div><div class="detail">'+d.content+'...<a class="more" href="javascript:void(0)">View more</a></div></div>'   
                }
            }
            this.contentEl[0].innerHTML=html;
        }
        this.changeOutSize(this.config.width,this.config.height);
        if(typeof this.callback.onLoad=='function'){
            this.callback.onLoad({
                height:this.el.height(),
                width:this.el.width(),
                component:'newsOverview',
                minWidth:this.minWidth
            });
        }
    },
    changeOutSize:function(w,h){
        if(!this.config.fixHeight){
            return;
        }
        var parent = this.el.parent();
        w = w>0?w:parent.width();
        h = h>0?h:parent.height();
        if(w<this.minWidth){
            w=this.minWidth;
        }
        if(w<=0||h<=0){
            return;
        }
//        this.width=w;
//        this.height=h;
//        this.contentEl.css({width:w,height:h});
        this.contentScrollPN.reSize(w,h-this.headHight);
    },
    clear:function(){
        this.el.unbind(".newOverview");
        for(var p in this){
            if(!p || p == 'clear'){continue;}
            this[p] = null;
            delete this[p];
        }
    }
};
